// Flash Object ID
var videoHolderName = 'player2holder';

// create SntPlayer object instance
var player = new SntPlayer(videoHolderName);

// helper function
function $$(id){ return document.getElementById(id) || null; }

// helper function to format output 
function formatTime(seconds){
	var s = seconds;
	var m = 0;
	if(seconds > 60){
		m = Math.floor(seconds / 60)
		s = seconds % 60;
	}
	return (m < 10 ? '0' + m : m) + ':' + (s < 10 ? '0' + s : s);
};

// here we set player callback function
player.ready(function(r){
	// get control objects
	var play_button = $$('play-button');
	var stop_button = $$('stop-button');
	var play_time = $$('play-time');

	// play event - send play event to the player
	var playEvent = function(){
		player.sendMessage('uiPlayClick');
	};
	
	// pause event - send pause event to the player
	var pauseEvent = function(){
		player.sendMessage('uiPauseClick');
	};
	
	// stop event - send stop event to the player
	var stopEvent = function(){
		player.sendMessage('uiStopClick');
	};
	
	// play event listener
	var onPlayEvent = function(){
		// change text on the control
		play_button.value = 'pause';
		
		// change click handler
		play_button.onclick = pauseEvent;
		
		// enable stop button
		stop_button.removeAttribute('disabled');
	};
	
	// pause event listener
	var onPauseEvent = function(){
		// change text on the control 
		play_button.value = 'play';
		
		// change click handler
		play_button.onclick = playEvent;
	};
	
	// stop event listener and some other system events
	var onStopEvent = function(){
		// disable stop button
		stop_button.setAttribute('disabled', 'disabled');

		// simulate pause event 
		onPauseEvent();
	};
	
	
	
	// media state listener - current playing position
	var onStateChange = function(s){
		// round position
		var position = Math.ceil(s.position);
		
		// show formatted position in text field
		play_time.value = formatTime(position);		
	};	
		
	// add event listeners to the player
	player.addEventListener('mediaPlay', onPlayEvent);
	// add event to receive video status every second
	player.addEventListener([ 'mediaStop', 'loadingStop', 'mediaStopStartPosition' ], onStopEvent);
	player.addEventListener('mediaStateChange', onStateChange);
	
	// add event handler to our controls
	play_button.onclick = playEvent;
	stop_button.onclick = stopEvent;
});
